
var Emitter = require( 'emitter-io')

module.exports = (app) => {
    const BaseService = require('egg').Service;

    class mqtt extends BaseService {
        async GenerateKey( channel, permission  ){ 
            return new Promise( ( resolve, reject )=>{ 
                try{ 
                  app.emitter.on( 'keygen', function( channelKey ) { 
                      console.log( 'keygen return  ', channelKey )
                      //return channel key 
                      resolve( channelKey.key );

                  })

                  var request = { key: app.config.mqtt.masterKey, channel: channel, type: permission }
                  app.emitter.keygen( request )
                } catch( error ) { 
                    reject( { error }  )
                }
            })
        }
    }
    return mqtt
}

